package com.edeweiss.community.servlet;

import com.edeweiss.community.domain.Activity;
import com.edeweiss.community.services.ActivityService;
import com.edeweiss.community.services.servicesImp.ActivityServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.scene.input.DataFormat;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/ActivityServlet")
public class ActivityServlet extends BaseServlet {

    private ActivityService activityService = new ActivityServiceImpl();
    //社团向社联申请举办活动
    public String applyConductActivity(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取申请的信息
        String aname = request.getParameter("aname");//活动名称
        String atime = request.getParameter("atime");//活动举办的时间
        String aplace = request.getParameter("aplace");//活动举办的地点
        String community_id = request.getParameter("community_id");//举办的社团id
        String aintroduce = request.getParameter("aintroduce");//活动介绍
        String money = request.getParameter("money");//所需经费
        String budget = request.getParameter("budget");//经费预算信息

        Activity a = new Activity();
        a.setAname(aname);
        a.setAintroduce(aintroduce);
        a.setAplace(aplace);
        a.setMoney(Double.parseDouble(money));
        a.setAstatus(0);//设置处理状态，0未处理
        a.setBudget(budget);
        a.setCommunity_id(Integer.parseInt(community_id));

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(atime);
            a.setAtime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //2.调用业务层方法进行申请
        int flag = activityService.applyConductActivity(a);
        if(flag == 1){
            request.getSession().setAttribute("msg","申请表已经提交，请耐心等候管理员处理！");
        }else {
            request.getSession().setAttribute("msg","申请失败，请重新申请！");
        }
        //3.跳转页面
        response.sendRedirect("community/index.jsp");
        return "";
    }
    //查询所有的历史活动，已经审批过的活动
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String status = request.getParameter("status");
        List<Activity> activities = activityService.findAllActivity(Integer.parseInt(status));
        if (activities != null && activities.size()>0) {
            request.getSession().setAttribute("activities",activities);
            response.sendRedirect("/pages/allActivity.jsp");
        }else {
            request.getSession().setAttribute("msg","暂无任何活动信息");
           response.sendRedirect("/pages/index.jsp");
        }
        return "";
    }

    //根据活动id删除活动
    public String delActivity(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String aid = request.getParameter("aid");
        int flag = activityService.delActivity(Integer.parseInt(aid));
        String msg = flag==1?"删除成功":"删除失败";
        request.getSession().setAttribute("msg",msg);
        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //审批活动,查询出所有未审批的活动
    public String findActivityApprove(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String status = request.getParameter("status");
        List<Activity> activities = activityService.findAllActivity(Integer.parseInt(status));
        if (activities != null && activities.size()>0) {
            request.getSession().setAttribute("activities",activities);
            response.sendRedirect("/pages/activityApprove.jsp");
        }else {
            request.getSession().setAttribute("msg","暂无任何申请举办活动信息");
            response.sendRedirect("/pages/index.jsp");
        }
        return "";
    }

    //管理员审批活动
    public String activityApprove(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //处理活动的id
        String aid = request.getParameter("aid");
        //处理的结果
        String result = request.getParameter("result");
        //获取社团id
        String cid = request.getParameter("community_id");

        //调用service层方法进行处理
        int flag = activityService.activityApprove(Integer.parseInt(aid), Integer.parseInt(result),Integer.parseInt(cid));
        String msg=flag==1?"处理成功，请接着处理下一条活动！":"处理失败，请重新尝试";
        request.getSession().setAttribute("msg",msg);
        //返回结果
        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //学生报名参加活动
    public String applyJoinActivity(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取要参加的活动id
        String aid = request.getParameter("aid");
        //获取要参加的学生的id
        String sid = request.getParameter("sid");
        int flag = activityService.applyJoinActivity(Integer.parseInt(sid), Integer.parseInt(aid));
        String msg = flag == 1?"申请参加活动成功，请耐心等待管理员回复！":"申请参加活动失败，请重新尝试！";

        //返回信息
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getOutputStream(),msg);

        return "";
    }
}
